import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ExcelExportService } from './excel-export.service';

@Component({
  selector: 'app-excel-test',
  standalone: true,
  imports: [CommonModule],
  template: `
    <div class="container mt-4">
      <h2>Excel导出测试</h2>
      <button class="btn btn-primary" (click)="testExport()" [disabled]="isLoading">
        {{ isLoading ? '导出中...' : '测试导出Excel' }}
      </button>
      <div class="mt-2" *ngIf="message">{{ message }}</div>
    </div>
  `
})
export class ExcelTestComponent {
  isLoading = false;
  message = '';

  constructor(private excelExportService: ExcelExportService) {}

  async testExport() {
    this.isLoading = true;
    this.message = '开始导出...';

    try {
      // 创建测试数据
      const testData = [
        {
          id: '1',
          name: '测试客户1',
          contactPerson: '联系人1',
          phone: '12345678',
          mobile: '13800138000',
          address: '测试地址1',
          enterpriseAttribute: '民营企业',
          contractNumber: 'C001',
          contact1: '备用联系人1',
          phone1: '87654321',
          emailAddress: 'test1@example.com',
          remarks: '测试备注1'
        },
        {
          id: '2',
          name: '测试客户2',
          contactPerson: '联系人2',
          phone: '12345679',
          mobile: '13800138001',
          address: '测试地址2',
          enterpriseAttribute: '国有企业',
          contractNumber: 'C002',
          contact1: '备用联系人2',
          phone1: '87654322',
          emailAddress: 'test2@example.com',
          remarks: '测试备注2'
        }
      ];

      console.log('导出测试数据', testData);
      
      // 导出Excel
      await this.excelExportService.exportKehuToExcel(testData, '测试导出');
      
      this.message = '导出成功！';
      console.log('导出完成');
    } catch (error) {
      console.error('导出失败', error);
      this.message = `导出失败：${error.message || '未知错误'}`;
    } finally {
      this.isLoading = false;
    }
  }
} 